package com.cloudcc.mobile.view.market;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;
import butterknife.OnClick;

import com.cloudcc.cloudframe.bus.EventEngine;
import com.cloudcc.mobile.ExtraConstant;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.event.MarketActivityEventList.MarketCheckByPhoneEvent;
import com.cloudcc.mobile.event.MarketActivityEventList.MarketRefreshInfo;
import com.cloudcc.mobile.presenter.MarketActivityPresenter;
import com.cloudcc.mobile.util.ViewUtils;
import com.cloudcc.mobile.view.base.BaseFragment;
import com.cloudcc.mobile.widget.CloudCCTitleBar;
import com.cloudcc.mobile.widget.CloudCCTitleBar.OnTitleBarClickListener;

public class MarketCheckPhoneFragment extends BaseFragment implements
		OnTitleBarClickListener {

	@Bind(R.id.market_tv_phone_name)
	TextView market_tv_phone_name;
	@Bind(R.id.market_tv_phone_number)
	TextView market_tv_phone_number;
	@Bind(R.id.market_btn_check)
	Button market_btn_check;

	@Bind(R.id.headerbar)
	CloudCCTitleBar headerbar;

	private MarketActivityPresenter mMarketPresenter = new MarketActivityPresenter();

	private String mMarketActivityId;
	private String mPhoneNumber;
	private String mName;

	@Override
	public void init() {
		initHeader();
		refreshInfoData();
		headerbar.completeProgress();
	}

	/**
	 * 初始化标题
	 */
	private void initHeader() {
		mPhoneNumber = getArguments().getString("market.phone", "");
		mName = getArguments().getString("market.name", "");
		mMarketActivityId = getArguments()
				.getString(ExtraConstant.EXTRA_ID, "");
		headerbar.setOnTitleBarClickListener(this);
	}

	/**
	 * 刷新info 数据
	 */
	private void refreshInfoData() {
		ViewUtils.setText(market_tv_phone_name,mName);
		ViewUtils.setText(market_tv_phone_number,mPhoneNumber);
		market_btn_check.setEnabled(true);

	}

	@OnClick(R.id.market_btn_check)
	public void onClickChect() {
		showWainting("请稍等,正在签到...");
		mMarketPresenter.checkByPhone(mPhoneNumber, mMarketActivityId);
	}

	@OnClick(R.id.market_btn_back)
	public void onClickFinish() {// 返回
		onClickLeft(null);
	}

	public void onEventMainThread(MarketCheckByPhoneEvent event) {
		dismissWainting();
		headerbar.completeProgress();
		if (event.isError()) {
			Toast.makeText(mContext, "亲,签到失败~~", 0).show();
			return;
		}
		showToast("签到成功...");
		onClickLeft(null);
		//刷新市场活动详情
		MarketRefreshInfo marketRefreshInfo = new MarketRefreshInfo();
		marketRefreshInfo.setOk(true);
		EventEngine.post(marketRefreshInfo);
	}

	@Override
	public void onClickLeft(View v) {
		getActivity().finish();
	}

	@Override
	public void onClickRight(View v) {

	}

	@Override
	public int getLayoutId() {
		return R.layout.market_checkphone_frag;
	}

	@Override
	protected boolean hasEventLife() {
		return true;
	}

}
